import Foundation
import UIKit
import SnapKit
import Alamofire

class GVCollection:UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    //全局变量
    public let cellHeight:Int = 52
    public var taskItems:[TaskInofItem]=[]
//    private let api = "/growthvalue/tasklist"
    
    
    //初始化方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        attributeInit()
    }
    convenience init(frame: CGRect) {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        self.init(frame: frame, collectionViewLayout: layout)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //属性配置
    func attributeInit(){
        isScrollEnabled = true
        
        backgroundColor = .white
        layer.cornerRadius = 15
        
        dataSource = self
        delegate = self
        register(TaskGVCell.self, forCellWithReuseIdentifier: "TaskGVCell")
        register(TaskHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "taskHeader")
    }
    
    //collection函数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return taskItems.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TaskGVCell", for: indexPath)as! TaskGVCell
        let  item = taskItems[indexPath.row]

        if !item.sideImage.isEmpty {
            cell.configure(image: UIImage(named: item.imageOfTask) ?? UIImage(named: "成长值-家园通")!,
                           nameOfTask: item.nameOfTask,
                           detailOfTask: item.detailOfTask,
                           numOfButton: item.numOfButton,
                           sideImg: item.sideImage)
        }else{
            cell.configure(image: UIImage(named: item.imageOfTask) ?? UIImage(named: "成长值-家园通")!,
                           nameOfTask: item.nameOfTask,
                           detailOfTask: item.detailOfTask,
                           numOfButton: item.numOfButton)
        }
        cell.delegate = self
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let header = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: "taskHeader",
                for: indexPath)as! TaskHeaderView
            return header
        }
        return UICollectionReusableView()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 307, height: 52);
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 0)
    }
    weak var delegateCollection: CollectionDelegate?
    protocol CollectionDelegate: AnyObject {
        func collectionToView(text:String)
    }
}

extension GVCollection: TaskGVCell.CellDelegate {
    func didTapButtonInCell(in cell:TaskGVCell) {
        delegateCollection?.collectionToView(text:cell.taskName.text!)
    }
}

